Completed
Pull Request — master (#77)
by Sander
01:21
created

parseTLD.js ➔ parse_host   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 31
rs 8.439
1
var parse_host = function(host){
2
    if(typeof tlds === "undefined"){
0 ignored issues
show
Bug introduced by
The variable tlds seems to be never declared. If this is a global, consider adding a /** global: tlds */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
3
        throw new Error('No TLDs!');
4
    }
5
6
    var parts = host.split(".");
7
    var stack = "";
8
    var tld_level = 1; //unknown tld are 1st level
9
    for(var i=parts.length-1, part;i>=0;i--){
10
        part = parts[i];
11
        stack = stack ? part + "." + stack : part;
12
        if(!tlds[stack]){
13
            break;
14
        }
15
        tld_level = tlds[stack];
16
    }
17
    if(parts.length <= tld_level ) {
18
        return {
19
            tld: null,
20
            domain: host
21
        };
22
    } else {
23
        return  {
24
            tld     : parts.slice(-tld_level).join('.'),
25
            domain  : parts.slice(-tld_level-1).join('.'),
26
            sub     : parts.slice(0, (-tld_level-1)).join('.'),
27
        };
28
    }
29
30
31
};